package com.sympsel.mySort;

import java.util.Random;

public class Sort<T> {
    private Sort() {
    }

    public static <T> void swap(T[] arr, int i, int j) {
        if (i != j) {
            T temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    public static <T extends Comparable<T>> void bubbleSort(T[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i].compareTo(arr[j]) > 0) {
                    swap(arr, i, j);
                }
            }
        }
    }

    public static <T extends Comparable<T>> void selectSort(T[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[minIndex].compareTo(arr[j]) > 0) {
                    minIndex = j;
                }
            }
            swap(arr, i, minIndex);
        }
    }

    public static <T extends Comparable<T>> void insertSort(T[] arr) {
        for (int i = 1; i < arr.length; i++) {
            T min = arr[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (arr[j].compareTo(min) > 0) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = min;
        }
    }

    public static <T extends Comparable<T>> void shellSort(T[] arr) {
        int gap = arr.length;
        while (gap > 1) {
            gap /= 2;
            for (int i = gap; i < arr.length; i++) {
                T min = arr[i];
                int j = i - gap;
                for (; j >= 0; j -= gap) {
                    if (arr[j].compareTo(min) > 0) {
                        arr[j + gap] = arr[j];
                    } else {
                        break;
                    }
                }
                arr[j + gap] = min;
            }
        }
    }

    public static <T extends Comparable<T>> void mergeSort(T[] arr) {
        int len = arr.length;
        @SuppressWarnings("unchecked")
        T[] temp = (T[]) java.lang.reflect.Array.newInstance(arr.getClass().getComponentType(), len);
        mergeSort(arr, 0, len - 1, temp);
    }

    private static <T extends Comparable<T>> void mergeSort(T[] arr, int begin, int end, T[] temp) {
        if (begin >= end) {
            return;
        }
        int mid = begin + ((end - begin) >> 1);
        mergeSort(arr, begin, mid, temp);
        mergeSort(arr, mid + 1, end, temp);
        merge(arr, begin, mid, end, temp);
    }

    private static <T extends Comparable<T>> void merge(T[] arr, int begin, int mid, int end, T[] temp) {
        int k = 0;
        int left = begin, right = mid + 1;
        while (left <= mid && right <= end) {
            temp[k++] = arr[left].compareTo(arr[right]) > 0 ? arr[right++] : arr[left++];
        }

        while (left <= mid) {
            temp[k++] = arr[left++];
        }

        while (right <= end) {
            temp[k++] = arr[right++];
        }

        if (k >= 0) System.arraycopy(temp, 0, arr, begin, k);
    }

    public static<T extends Comparable<T>> void quickSort(T[] arr) {
        quickSort(arr, 0, arr.length - 1);
    }

    private static <T extends Comparable<T>> void quickSort(T[] arr, int begin, int end) {
        if (begin >= end) {
            return;
        }
        T key = arr[new Random().nextInt(end - begin + 1) + begin];
        int left = begin, right = end;
        for (int i = begin; i <= right; ) {
            int cmp = key.compareTo(arr[i]);
            if (cmp > 0) {
                swap(arr, i++, left++);
            } else if (cmp == 0) {
                i++;
            } else {
                swap(arr, i, right--);
            }
        }
        quickSort(arr, begin, left - 1);
        quickSort(arr, right + 1, end);
    }

    public static<T extends Comparable<T>> void heapSort(T[] arr) {
        for (int i = (arr.length - 2) / 2; i >= 0; i--) {
            adjustDown(arr, arr.length, i);
        }

        for (int i = arr.length - 1; i >= 0; i--) {
            swap(arr, 0, i);
            adjustDown(arr, i, 0);
        }
    }

    private static <T extends Comparable<T>> void adjustDown(T[] arr, int len, int root) {
        int child = root * 2 + 1;
        while (child < len) {
            if (child + 1 < len && arr[child + 1].compareTo(arr[child]) > 0) {
                child++;
            }

            if (arr[child].compareTo(arr[root]) > 0) {
                swap(arr, child, root);
                root = child;
                child = root * 2 + 1;
            } else {
                break;
            }
        }
    }
}
